﻿//获取url参数
function getUrlParam(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象;
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r!=null) return unescape(r[2]); return null; //返回参数值
};

//js获取上下文路径
var webroot=document.location.href;
webroot=webroot.substring(webroot.indexOf('//')+2,webroot.length);
	webroot=webroot.substring(webroot.indexOf('/')+1,webroot.length);
webroot=webroot.substring(0,webroot.indexOf('/'));
rootPath="/"+webroot;


//my97  服务期客户到期时间的开始时间
function setStartTime() {
	var endTime = $('#endTime').val();
	WdatePicker({dateFmt:'yyyy-MM-dd', maxDate:endTime, firstDayOfWeek:1,isShowWeek:true});
}

//my97  服务期客户到期时间的结束时间
function setEndTime() {
	var startTime = $('#startTime').val();
	WdatePicker({dateFmt:'yyyy-MM-dd', minDate:startTime, firstDayOfWeek:1,isShowWeek:true});
}


//客户管理表单验证
function checkForm() {
//	var phone = $('#phone').val();
//	var email = $('#email').val();
//	var phoneReg =/^(13[0-9]|15[0-9]|18[0-9])[0-9]{8}$/;
//	var emailReg=/^[_a-z 0-9]+@([_a-z 0-9]+\.)+[a-z 0-9]{2,3}$/; 
//	
//	if(phone!= "" && !phoneReg.test(phone))
//	{ 
//		alert("请正确填写手机号码!");
//		$('#phone').focus();
//		return false;
//	}
//	
//	if(email && !emailReg.test(email))
//	{ 
//		alert("请正确填写邮箱地址!");
//		$('#email').focus();
//		return false;
//	}
	return true;
}



//推送客户前做的操作
function showPush(customId, pushHistoryProductId) {
	
	$('#pushCustomerId').val(customerId);
	$.post(
		rootPath + "/customerManage/push",
		{
			"customId": customerId,
			"pushHistoryProductId": pushHistoryProductId
		},
		function(data) {
			var json = eval(data);
			if(1 == json) {
				alert('已达月推送客户数量上限！');
				return ;
			}
			var length = json.length;
			if(length == 0) {
				alert("该客户已被推送过所有商品线，不可再进行推送!");
				return ;
			}else {
				
				var html = "";
				html += '<table align="center" cellspacing="10" style="width: 300px;">';
				html += 	"<tr>";
				html += 		"<td>";
				html += 			"商品类型：";
				html += 		"</td>";
				html += 	"<td>";
				for(var i=0; i<json.length; i++) {
					
					html += 	'<input type="checkbox" name="productIds" value="' + json[i].productCode + '" />';
					html += 	json[i].productName + "  ";
					
				}
				html += 	"</td>";
				html += 	"</tr>";
				html += 	"<tr>";
				html += 		"<td>";
				html += 			"推送备注：";
				html += 		"</td>";
				html += 		"<td>";
				html += 			'<input type="text" id="pushRemark" />';
				html += 		"</td>";
				html += 	"</tr>";
				html += 	"<tr>";
				html += 		"<td>";
				html += 		"</td>";
				html += 		"<td>";
				html += 			'<div class="btnOper2 btn_inline"><a href="#" onclick="doPush()" class="btn2"><span>推送</span></a>&nbsp;&nbsp;&nbsp;</div>'+
				'<div class="btnOper2 btn_inline"><a href="#" onclick="closeWindow('+ "'configBatchInfo'" +')" class="btn2"><span>关闭</span></a>&nbsp;&nbsp;&nbsp;</div>';
				html += 		"</td>";
				html += 	"</tr>";
				html += "</table>";
				$('#configBatchInfo').html(html);
				$('#configBatchInfo').show();
				$(".bg").fadeIn(100);
			}
		}
	);
	
}


//开始推送客户
function doPush() {
	var customerId = $('#pushCustomerId').val();
	var pushRemark = $('#pushRemark').val();
	var box = $('#configBatchInfo :checkbox');
	var idStr = "";
	for(var i=0; i< box.size(); i++) {
		if(box[i].checked) {
			idStr += box[i].value + ":";
		}
	}
	idStr = idStr.substring(0, idStr.length-1);
	if(idStr == "") {
		alert("请至少选择一项！");
		return ;
	}

	$.post(
			rootPath + "/customerManage/doPush",
			{
				"cusId": customerId,
				"productLabel": idStr ,
				"remark": pushRemark
			},
			function(data) {
				alert(data);
				$('#configBatchInfo').hide();
				$(".bg").fadeOut(100);
			}
		);
}

//接受/拒绝 单个客户
function selectedOne(obj, flag) {
	var box = $(obj).parent().parent().find(":checkbox");
	box.attr('checked', true);
	//flag == 1--接收 否则 拒绝
	if(flag == "1") {
		acceptSelected();
	}else {
		rejectSelected();
	}
	
}



//接收选中客户
function acceptSelected() {
	var customerType = $('#customerType').val();
	
	var idbox = $('#dataList :checkbox');
	
	var ecrIdStr = "";
	var cusIdStr = "";
	var empIdStr = "";
	var productIdStr = "";
	var flag = 0;
	
	for(var i=0; i< idbox.size(); i++) {
		if(idbox[i].checked && idbox[i].value != "on") {
			var idArray = idbox[i].value.split(":");
			ecrIdStr += idArray[0] + ":";
			cusIdStr += idArray[1] + ":";
			empIdStr += idArray[2] + ":";
			productIdStr += idArray[3] + ":";
			flag = 1;
		}
	}
	
	if(flag == 0) {
		alert("至少选择一项！");
		return ;
	}
	
	if(!confirm("确定接收？")){
		idbox.attr('checked', false);
		return ;
	}
	
	$.post(
			rootPath + "/customerManage/acceptCustomer",
			{
				"ecrIdStr": ecrIdStr,
				"cusIdStr": cusIdStr,
				"empIdStr": empIdStr,
				"productIdStr": productIdStr,
				"customerType": customerType
			},
			function(data) {
				var result = eval(data);
				if(result == 0) {
					alert("超时未接收！该客户已流走！");
				}
				query($('#pageNow').val());
			}
		);
	
}


//拒绝选中客户
function rejectSelected() {
	var customerType = $('#customerType').val();
	var idbox = $('#dataList :checkbox');
	
	var ecrIdStr = "";
	var cusIdStr = ""
	var empIdStr = ""
	var flag = 0;
	for(var i=0; i< idbox.size(); i++) {
		if(idbox[i].checked && idbox[i].value != "on") {
			var idArray = idbox[i].value.split(":");
			ecrIdStr += idArray[0] + ":";
			cusIdStr += idArray[1] + ":";
			empIdStr += idArray[2] + ":";
			flag = 1;
		}
	}
	
	if(flag == 0) {
		alert("至少选择一项！");
		return ;
	}
	
	if(!confirm("确定拒绝？")){
		idbox.attr('checked', false);
		return ;
	}
	
	$.post(
			rootPath + "/customerManage/rejectCustomer",
			{
				"ecrIdStr": ecrIdStr,
				"cusIdStr": cusIdStr,
				"empIdStr": empIdStr,
				"customerType": customerType
			},
			function(data) {
				var result = eval(data);
				if(result == 0) {
					alert("超时未接收！该客户已流走！");
				}
				query(customerType);
			}
		);
	
}


//弹出添加记录窗口
function showAddRemark(customerId) {
	$('#remark').val('');
	$('#remarkCustomerId').val(customerId);
	$('#addCustomerRemark').show();
	$(".bg").fadeIn(100);
}

//开始添加记录
function doAddRemark() {
	var customerId = $('#addCustomerRemark #remarkCustomerId').val();
	var remark = $('#addCustomerRemark #remark').val();
	if(remark == "") {
		alert("记录内容不能为空！");
		return ;
	}
	var reg = /[<>]/;
	if(reg.test(remark)) {
		alert("你想干嘛?");
		return ;
	}
	
	$.post(
			rootPath + "/customerManage/addCustomerRemark",
			{
				"cusId": customerId,
				"remark": remark
			},
			function(data) {
				$('#addCustomerRemark').hide();
				$(".bg").fadeOut(100);
			}
		);
}

//弹出修改记录窗口
function showModifyRemark(crId, remark, customerId) {
	$('#modifyCustomerRemark #remarkCustomerId').val(crId);
	$('#modifyCustomerRemark #remark').val(remark);
	$('#modifyCustomerRemark #customerId').val(customerId);
	$('#modifyCustomerRemark').show();
	$(".bg").fadeIn(100);
}

//修改记录
function doModifyRemark() {
	var id = $('#modifyCustomerRemark #remarkCustomerId').val();
	var remark = $('#modifyCustomerRemark #remark').val();
	var customId = $('#modifyCustomerRemark #customerId').val();
	
	if(remark == "") {
		alert("记录内容不能为空！");
		return ;
	}
	var reg = /[<>]/;
	if(reg.test(remark)) {
		alert("你想干嘛?");
		return ;
	}
	
	$.post(
		rootPath + "/customerManage/modifyCustomerRemark",
		{
			"id": id,
			"remark": remark
		},
		function(data) {
			$('#modifyCustomerRemark').hide();
			queryRemark(customId);
		}
	);
}

//查看记录
function queryRemark(cusId) {
	$(".bg").fadeIn(100);
	$('#queryCustomerRemark').show();
	$.post(
		rootPath + "/customerManage/queryCustomerRemark",
		{
			"cusId": cusId
		},
		function(data) {
			var json = eval(data);
			var html = "";
			html += "<tr>";
			html += 	"<th>记录内容</th>";
			html += 	"<th>记录时间</th>";
			/*html += 	"<th>操作</th>";*/
			html += "</tr>";
			
			for(var i=0; i<json.length; i++) {
				html += "<tr>";
				html += 	"<td>" + json[i].remark + "</td>";
				html += 	"<td>" + json[i].addTime + "</td>";
				html += 	'<td><a href="#" onclick="showModifyRemark('+"'" + json[i].id + "'," +
																	 "'" + json[i].remark + "'," +
																	 /*"'" + json[i].cusId + "'" +*/
																  ')"></a></td>';
				html += "</tr>";
			}
			html += "<tr>";
			html += 	"<td></td>";
			html += 	'<td><div class="btnOper2 btn_inline"><a href="#" onclick="closeWindow('+"'queryCustomerRemark'"+')" class="btn2"><span>关闭</span></a>&nbsp;&nbsp;&nbsp;</div></td>';
			html += 	"<td></td>";
			html += "</tr>";
			
			$('#remarkDataList').html(html);
		}
	);
}


//前往客户详情界面
function goCustomerDetails(customId) {
	var customerType = $('#customerType').val();
	url = rootPath + "/page/goCustomerDetails?customId=" + customId+"&customerType=" + customerType;
	window.open(url);
}


//关闭窗口
function closeWindow(window,flag) {
	$('#' + window).hide();
	if(!flag) {
		$(".bg").fadeOut(100);
	}
}



//复选框全选
function selectedBox(obj) {
	$('#dataList :checkbox').each(function() {
		this.checked = obj.checked;
	});
}


//跳到输入的分页
function goInputPage() {
	var inputPage = $('#inputPage').val();
	var reg = /^[0-9]+$/;
	if(inputPage == "") {
		return ;
	}
	if(!reg.test(inputPage)) {
		alert("请输入正确的页数");
		return ;
	}
	
	query(inputPage);
	
}


//初始化所属部门树
function initTree() {
	var setting = {
			data : {
				Key : { name : "name" },
				simpleData : { enable : true, idKey : "id", pIdKey : "pId" }
			},
			callback : { onClick : zTreeClick }
	};
	var treeNodes = deptJson;
	$.fn.zTree.init( $('#treeDemo'), setting, treeNodes );
}


//点击函数
function zTreeClick(event, treeId,treeNode) {
		
	   currentSelectedNode = treeNode;
	   
	   var deptId = treeNode.id;
	   $('#deptId').val(deptId);
	   $.post(
		   rootPath + "/customerManage/checkIsLeaf",
		   {
			   "deptId": deptId
		   },
		   function(data) {
			   var json = eval(data);
			   
			   if(data != 1) {
				   $("#dept").val(treeNode.name);
				   hideMenu();
				   
				  
				   var html = '<option value="" >请选择</option>';
				   var length = json.length;
				   for(var i = 0; i < json.length; i ++) {
					   html += '<option value="'+ json[i].empno +'">'+ json[i].ename +'</option>';
				   }
				   if(length != 0) {
					   $('#emp').html(html);
				   }else {
					   $('#emp').html('<option value="">暂无销售人员</option>');
				   }
			   }
		   }
	   );
	   
};



//展示所属部门树
function showMenu() {
	var dept = $("#dept");
	var deptOffset = $("#dept").offset();
	$("#menuContent").css({left:deptOffset.left + "px", top:deptOffset.top + dept.outerHeight() + "px"}).slideDown("fast");

	$("body").bind("mousedown", onBodyDown);
}

//隐藏所属部门树
function hideMenu() {
	$("#menuContent").fadeOut("fast");
	$("body").unbind("mousedown", onBodyDown);
}

//任意点击body一个地方隐藏所属部门树
function onBodyDown(event) {
	if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
		hideMenu();
	}
}


//清空表单
function doReset() {
	$('#emp').html('<option value="">请选择</option>');
	$('#area').html('<option value="">请选择</option>');
	$('#empIdStr').val('');
	$('#form').get(0).reset(); 
}

//打开过关闭 等待弹窗
function controllerWaitingBar(flag) {
	var hwnd=$("#waitDiv");
	if(flag)
		hwnd.fadeIn(200);
	else
		hwnd.fadeOut(200);
}

